<?xml version="1.0" encoding="UTF-8" ?>

<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>

<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>

<%@page import="insight.google.game.era.data.Host"%>
<%@page import="insight.google.game.era.data.Room"%>
<%@page import="insight.google.game.era.data.Player"%>

<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        String userID = null;
        Map<String, Player> userMap = null;
        Host host = (Host) application.getAttribute(Host.class.getName());
        boolean isJoinedRoom = false;
        Room joinedRoom = null;
        if (user == null) {
        	response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
        	return;
        } else {
        	userID = user.getUserId();
        	userMap = (Map<String, Player>) application.getAttribute(Player.class.getName());
        	Player player = (Player) userMap.get(user.getUserId());
        	if ((host == null) || (player == null)) {
        		response.sendRedirect(request.getRequestURL().substring(0, request.getRequestURL().indexOf("/insightgoogle")) + "/insightgoogle/login");
        		return;
        	}
        	joinedRoom = player.getRoom();
        	if (joinedRoom != null) {
        		isJoinedRoom = true;
        	} else {
        		String roomName = request.getParameter("room");
        		if ((roomName != null) && !roomName.equals("")) {
        			host.joinRoom(roomName, player);
        			isJoinedRoom = true;
        			joinedRoom = player.getRoom();
        		}
        	}
        }
%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j" />
<title>Welcome to Game Center</title>
</head>
<body>
	<A href ="/insightgoogle/logout">Logout</A>
	<table width="98%" align="center">
		<tr>
			<td align="center" colspan="4">
				Room List
			</td>
		</tr>
		<%
			if (host != null) {
				Iterator<Room> roomIterator = host.getRooms();
				int i = 0;
				Room room;
				while (roomIterator.hasNext()) {
					room = roomIterator.next();
					i++;
		%>
					<tr bgcolor='<%= (i % 2 == 0) ? "#dddddd" : "#eeeeee"%>'>
						<td>
							<%=room.getName()%>			
						</td>
						<td>
							<%
								if (isJoinedRoom) {
									if (room == joinedRoom) {
										out.println("Joined");
									} else {
										out.println("&nbsp;");
									}
								} else {
									out.println("<A href='GameCenter.jsp?room=" + room.getName() + "'>Join</A>");
								}
							%>
						</td>
						<td>
							Online Users:<br/>
							<%
								int number = 0;
								List<Player> playerList = room.getPlayerList();
								for (Player player : playerList) {
									out.print("Player " + number++ + ": " + player + "<br/><br/>");
								}
							%>
						</td>
						<td>
							Owner: <%=room.getOwner()%>							
						</td>
					</tr>
		<%
				}
			}
			
		%>
	</table>
</body>
</html>